/*
 * @Author: new-wang
 * @Date: 2025-08-29 16:35:35
 * @LastEditors: new-wang
 * @LastEditTime: 2025-09-02 16:15:20
 * @FilePath: \nest-demo\src\entityAll\entities\UserRoles.ts
 * @Description: 用户-角色关系表实体
 */
import {
  Column,
  Entity,
  Index,
  JoinColumn,
  ManyToOne,
  PrimaryGeneratedColumn,
} from "typeorm";
import { Roles } from "./Roles";
import { Systems } from "./Systems";
import { Users } from "./Users";

@Index("fk_user_roles_role_id", ["roleId"], {})
@Index("fk_user_roles_system_id", ["systemId"], {})
@Index("uniq_user_role_system", ["userId", "roleId", "systemId"], {
  unique: true,
})
@Entity("user_roles", { schema: "nest-demo" })
export class UserRoles {
  @PrimaryGeneratedColumn({ type: "bigint", name: "id", unsigned: true })
  id: number;

  @Column("bigint", { name: "user_id", comment: "用户ID", unsigned: true })
  userId: number;

  @Column("int", { name: "role_id", comment: "角色ID", unsigned: true })
  roleId: number;

  @Column("int", { name: "system_id", comment: "系统ID", unsigned: true })
  systemId: number;

  @Column("timestamp", {
    name: "created_at",
    nullable: true,
    default: () => "CURRENT_TIMESTAMP",
  })
  createdAt: Date | null;

  @Column("bigint", {
    name: "create_by",
    nullable: true,
    comment: "权限分配人ID - 由谁创建",
    unsigned: true,
  })
  createBy: string | null;

  @ManyToOne(() => Roles, (role) => role.userRoles, {
    onDelete: "CASCADE",
    onUpdate: "NO ACTION",
  })
  @JoinColumn([{ name: "role_id", referencedColumnName: "id" }])
  role: Roles;

  @ManyToOne(() => Systems, (systems) => systems.userRoles, {
    onDelete: "CASCADE",
    onUpdate: "RESTRICT",
  })
  @JoinColumn([{ name: "system_id", referencedColumnName: "id" }])
  system: Systems;

  @ManyToOne(() => Users, (users) => users.userRoles, {
    onDelete: "CASCADE",
    onUpdate: "NO ACTION",
  })
  @JoinColumn([{ name: "user_id", referencedColumnName: "id" }])
  user: Users;
}
